﻿using Comm;
using GalaSoft.MvvmLight.Messaging;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Kinlo.UI.Main.View
{
    /// <summary>
    /// CheckUserDoView.xaml 的交互逻辑
    /// </summary>
    public partial class CheckUserDoView : Window
    {
        public CheckUserDoView()
        {
            InitializeComponent();
            this.userTxt.Text = GlobalValues.EmpNo;
            this.equTxt.Text = GlobalValues.EquNo;
            this.MesOnOff.IsChecked = GlobalValues.MesOnOff;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var user = this.userTxt.Text.Trim();
            var pass = this.passTxt.Password;
            var equ = this.equTxt.Text.Trim();
            if (string.IsNullOrEmpty(user) || string.IsNullOrEmpty(pass) || string.IsNullOrEmpty(equ))
            {
                MessageBox.Show("用户名,密码,设备编号都不能为空", "错误");
                return;
            }
            var returnVal = Kinlo.SWDLib.Lib.CheckUserDo(user, pass, equ);
            Logger.Info(string.Format("用户校验结果:{0}", returnVal));
            if (returnVal.ToUpper().Contains("TRUE"))
            {
                this.DialogResult = true;
                GlobalValues.EmpNo = user;
                GlobalValues.EquNo = equ;
                Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                if (config.AppSettings.Settings.AllKeys.Contains("EquNo"))
                {
                    config.AppSettings.Settings["EquNo"].Value = equ;
                }
                else
                {
                    config.AppSettings.Settings.Add("EquNo", equ);
                }
                config.Save(ConfigurationSaveMode.Modified);
                ConfigurationManager.RefreshSection("appSettings");
                MessageBox.Show("设备登录成功", "信息");
                Messenger.Default.Send<string>("", "UpdateUIGloableValues");
                this.Close();
            }
            else
            {
                var msg = returnVal;
                var index = returnVal.IndexOf(':');
                if (index > 0)
                {
                    msg=returnVal.Substring(index+1);
                }
                MessageBox.Show(string.Format("设备登录失败,请重新录入 {0}",msg), "信息");

            }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }

        private void MesOnOff_Checked(object sender, RoutedEventArgs e)
        {
            // 当CheckBox被选中时触发的逻辑

            GlobalValues.MesOnOff=MesOnOff.IsChecked.Value;
            Logger.Info(string.Format("{0} 修改了【MES在线状态】参数 {1}", GlobalValues.EmpNo, MesOnOff.IsChecked.Value));
        }

        private void MesOnOff_Unchecked(object sender, RoutedEventArgs e)
        {
            // 当CheckBox被取消选中时触发的逻辑
            GlobalValues.MesOnOff = MesOnOff.IsChecked.Value;
            Logger.Info(string.Format("{0} 修改了【MES在线状态】参数 {1}", GlobalValues.EmpNo, MesOnOff.IsChecked.Value));
        }
    }
}
